import { createStore } from 'vuex'

export default createStore({
  state: {
    // 用户信息
    curUser: {
        id: '',
        name: '',
        token: '',
        level: '',
        score: 0,
    },
    curSearchItemIndex: 0,
    workList:[],
    searchContent: [
    //   {
    //   id:'',
    //   icon:'',
    //   name:'',
    //   desc:'',
    //   level:'',
    //   score:'',
    //   type:'',
    //   params: ''
    // }
    ],
    curSelPos: -1
  },
  mutations: {
    // 在这里定义你的mutations
  SETCURUSERINFO({curUser}, resp_data) {
        curUser.id = resp_data.id
        curUser.name = resp_data.name
        curUser.token = resp_data.token
        curUser.level = resp_data.level
        curUser.score = resp_data.score
    },
    ADDWORKITEM(state, item) {
      state.workList.push(item);
    },
    ADDSEARCHCONTENT(state, arr) {
      state.searchContent.length = 0;
      state.searchContent.push.apply(state.searchContent, arr);
    },
    SETSEARCHITEM(state, {id, data}){
      let pos = -1;
      for (let i = 0; i < state.searchContent.length; i++) {
        if (id == state.searchContent[i].id) {
          pos = i;
          break;
        }
      }
      console.log('pos', pos);
      if(pos == -1) {
        return;
      }
      let it = data;
      state.searchContent[pos].id = it.id;
      state.searchContent[pos].icon = it.icon;
      state.searchContent[pos].name = it.name;
      state.searchContent[pos].desc = it.desc;
      state.searchContent[pos].level = it.level;
      state.searchContent[pos].score = it.score;
      state.searchContent[pos].type = it.type
      console.log('params', it.params);
      state.searchContent[pos].params = it.params;
    },
    REMOVEWORKITEM(state, pos) {
      state.workList.splice(pos, 1);
    },
    SETTOKEN(state, token) {
      state.curUser.token = token;
    },
    SETWORKLIST(state, workList) {
      state.workList.length = 0;
      state.workList.push.apply(state.workList, workList);
    },
    SETCURSELPOS(state, pos) {
      state.curSelPos = pos;
    }
  },
  actions: {
    // 在这里定义你的actions
    setCurUserInfo({commit}, resp_data) {
      commit('SETCURUSERINFO', resp_data);
    },
    addWorkItem({commit}, item) {
        commit('ADDWORKITEM', item);
    },
    addSearchContent({commit}, arr) {
        commit('ADDSEARCHCONTENT', arr);
      },
    setSearchItem({commit}, obj) {
      commit('SETSEARCHITEM', obj);

    },
    removeworkItem({commit}, id) {
      commit('REMOVEWORKITEM', id);
    },
    setToken({commit}, token) {
      commit('SETTOKEN', token);
    },
    setWorkList({commit}, workList) {
      commit('SETWORKLIST', workList);
    },
    setCurSelPos({commit}, pos) {
      commit('SETCURSELPOS', pos);
    }
  },
  modules: {
   
  }


})